{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0892c7f3",
   "metadata": {},
   "source": [
    "* 记录人：曾烨"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f89df44b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 上周回顾\n",
    "## 函数的定义：def\n",
    "## 函数名的定义规则--》变量名一样的规则\n",
    "## 调用函数的方式\n",
    "## 参数：形式参数 实际参数\n",
    "## 有多个参数时： 位置参数--》关键字要放在末尾  默认参数--》默认参数均放在末尾\n",
    "\n",
    "\n",
    "# 本周知识\n",
    "## 函数的返回值\n",
    "## 函数的参数类型\n",
    "## 模块与函数的关系\n",
    "## 定义一个模块，然后调用一个模块：1.调用模块  2.调用模块中的函数\n",
    "\n",
    "## 函数的返回值\n",
    "### 返回值的关键字：return\n",
    "### 在其他的编程语言中，一般return一定要写，不写会报错，但是在python中，return可以不写，return默认为none\n",
    "### 编程语言的函数，一定要有返回值\n",
    "### 返回值的结果是一个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8c025a8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + '  '+ last_name\n",
    "    return full_name.title()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9bcb4f53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Li  Hua\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"Li\",\"hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a33ae364",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 函数的参数\n",
    "### 函数输入的参数：1.函数式可以输入复杂的参数的：函数参数为列表 函数参数为字典\n",
    "\n",
    "### 函数的返回结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f9f24a5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \"\"\"函数输入一个列表\"\"\"\n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b3c72361",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['lihua', 'hhh']\n"
     ]
    }
   ],
   "source": [
    "list_test([\"lihua\",\"hhh\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c757bf3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "httpin_get = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61ad7400-7e7d446e0b18d985586e61bc\"\n",
    "  }, \n",
    "  \"origin\": \"210.21.79.237\", \n",
    "  \"url\": \"http://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0d36a798",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict_test(request_header):\n",
    "    \"\"\"函数输入一个字典\"\"\"\n",
    "    print(request_header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c5a9d542",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Host': 'httpbin.org', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36', 'X-Amzn-Trace-Id': 'Root=1-61ad7400-7e7d446e0b18d985586e61bc'}\n"
     ]
    }
   ],
   "source": [
    "dict_test(httpin_get[\"headers\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c059d2f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 函数的返回的结果：有多个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4417d53f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word:str) ->set:\n",
    "    \"\"\"Return a boolean based on any vowels found\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    return found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "05bc987a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'e', 'o'}\n"
     ]
    }
   ],
   "source": [
    "word = search4vowels(\"hello ooo\")\n",
    "print (word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "1ae2c61a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_person(name,age,job):\n",
    "    \"\"\"返回一个字典，其中包含有关一个人的信息\"\"\"\n",
    "    person = {'name':name,'age':age,'job':job}\n",
    "    return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1ef1a5fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'lihhh', 'age': 28, 'job': 'student'}\n"
     ]
    }
   ],
   "source": [
    "person = build_person(\"lihhh\",28,\"student\")\n",
    "print(person)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "beb6ffba",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 函数传递任意数量的实参\n",
    "#### args  kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9fcb1ac6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def make_food(food,*topping):\n",
    "    return food,toppings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "8ddadcbd",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'toppings' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-33-3cb092120e14>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mbuildfood\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmake_food\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'pizza'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-26-5162c97cd67e>\u001b[0m in \u001b[0;36mmake_food\u001b[1;34m(food, *topping)\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmake_food\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfood\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mtopping\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mfood\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mtoppings\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'toppings' is not defined"
     ]
    }
   ],
   "source": [
    "buildfood = make_food('pizza')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "dd5a7bc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# kwargs 实践\n",
    "## kwargs 可以接受多个数据（键值对）==》关键字参数\n",
    "## kwargs可以将多个数据放在字典中\n",
    "def person(name,**info):\n",
    "    \"\"\"打印个人信息\"\"\"\n",
    "    return name,info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "259dd524",
   "metadata": {},
   "outputs": [],
   "source": [
    "build_person = person(\"lihua\",age=20,job=\"student\",address=\"gznfxy\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e06f868b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('lihua', {'age': 20, 'job': 'student', 'address': 'gznfxy'})\n"
     ]
    }
   ],
   "source": [
    "print(build_person)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "53d41730",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"检验文本中出现的元音字母\"\"\"\n",
    "    vowel = set(\"u\",'s')\n",
    "    result = vowels & set(word)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "1e0f504a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 模块\n",
    "## 用户不需要看底层代码\n",
    "## 调用模块的方法\n",
    "### 必须先调用模块才可以使用\n",
    "### import moudle_name 直接调用整个模块，此时模块的所有函数处于备用状态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "16549efb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"检验文本中出现的元音字母\"\"\"\n",
    "    vowels = set(\"aeiou\")\n",
    "    result = vowels & set(word)\n",
    "    return result\n",
    "\n",
    "def news_链接(url,新闻分类,page):\n",
    "    \"\"\"抓取学校新闻的所有链接\"\"\"\n",
    "    news_链接 = []\n",
    "    for i in range(0,int(page)):\n",
    "        if i == 0:\n",
    "            index_链接 = url + 新闻分类 + '/index.htm'\n",
    "            news_链接.append(index_链接)\n",
    "        else:\n",
    "            index_链接 = url + 新闻分类 + '/index'+ str(i)+ '.htm'\n",
    "            news_链接.append(index_链接)       \n",
    "    return news_链接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cb70856a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['https://www.nfu.edu.cn/xxyw/index.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index1.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index2.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index3.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index4.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index5.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index6.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index7.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index8.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index9.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index10.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index11.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index12.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index13.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index14.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index15.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index16.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index17.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index18.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index19.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index20.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index21.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index22.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index23.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index24.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index25.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index26.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index27.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index28.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index29.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index30.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index31.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index32.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index33.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index34.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index35.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index36.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index37.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index38.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index39.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index40.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index41.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index42.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index43.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index44.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index45.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index46.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index47.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index48.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index49.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index50.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index51.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index52.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index53.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index54.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index55.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index56.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index57.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index58.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index59.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index60.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index61.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index62.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index63.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index64.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index65.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index66.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index67.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index68.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index69.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index70.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index71.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index72.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index73.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index74.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index75.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index76.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index77.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index78.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index79.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index80.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index81.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index82.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index83.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index84.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index85.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index86.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index87.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index88.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index89.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index90.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index91.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index92.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index93.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index94.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index95.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index96.htm',\n",
       " 'https://www.nfu.edu.cn/xxyw/index97.htm']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "news_链接('https://www.nfu.edu.cn/','xxyw','98')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58dedb82",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
